home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / bbsdoors / ckit258.zip / PCBDEMO.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  16KB  |  328 lines

  1. /****************************************************************************
  2.  *   FILE PCBDEMO.C                                                         *
  3.  *   Created 01-MAY-1993            Rickie W. Belitz                        *
  4.  *                                  820 Brentwood Drive                     *
  5.  *                                  Maryville, Tennessee  37804             *
  6.  *                                  BBS (615) 690-8231                      *
  7.  *                                      (615) 690-7968                      *
  8.  *                                      (615) 690-7913                      *
  9.  *                                                                          *
  10.  *        This small demo demostates obtaining conference information       *
  11.  *   from the pcboard.sys file read by CKIT.  ck_pcbsysbuf is a pointer to  *
  12.  *   the buffer used by CKIT to read the pcboard.sys file to.  This same    *
  13.  *   buffer will be written to the pcboard.sys file when the door closes.   *
  14.  *   Therefore, any changes made to the pcboard.sys buffer (ck_pcbsysbuf)   *
  15.  *   will be recorded to pcboard.sys when the door closes.  This demo shows *
  16.  *   how to obtain conference information from the pcboard.sys file.        *
  17.  *   PCBoard conference information is stored in the pcboard.sys file       *
  18.  *   in two different locations using bitmaps.  For conferences             *
  19.  *   0-39 this offset is 66 for conferences the user has joined and offset  *
  20.  *   71 for conferences the users has scanned.  (5 bytes each).             *
  21.  *   For conferences 40 and greater this offset is 144 for conferences the  *
  22.  *   users joined and following this is conferences the users has scanned.  *
  23.  *                                                                          *
  24.  *  The upper two conference fields are bit mapped just like the two fields *
  25.  *  at offsets 66 and 71 respectively. One key difference with these,       *
  26.  *  however, is that they apply only to conferences beyond conference #39   *
  27.  *  and that they  are dynamically sized at the byte level depending on     *
  28.  *  how many conferences the sysop is running on his PCBoard system.        *
  29.  *  Examples:                                                               *
  30.  *                                                                          *
  31.  *    High Conf Number    Extended Conferences    Dynamic Size (bytes)      *
  32.  *    ----------------    --------------------    ------------              *
  33.  *         < 39                    0                   0                    *
  34.  *          40                     1                   1                    *
  35.  *          50                    11                   2                    *
  36.  *         100                    61                   8                    *
  37.  *                                                                          *
  38.  *  The demo opens pcboard.dat and will expect an enviroment variable to    *
  39.  *  find where the file is located and the name.                            *
  40.  *  i.e. SET PCBDAT=C:\PCB\PCBOARD.DAT.  It then will open the pcboard.dat  *
  41.  *  file to determine the max. number of conferences the sysop has allocted.*
  42.  *  It will simply display each conference the user has joined and scanned  *
  43.  *  for messages.                                                           *
  44.  *                                                                          *
  45.  *  Use freely and modify as needed for use in conjunction with the         *
  46.  *  CKIT door programming libs.                                             *
  47.  ****************************************************************************/
  48. #include    "pcbdemo.h"
  49.  
  50. /******************************************************************************
  51.  *                     Start of Main Program                                  *
  52.  *****************************************************************************/
  53. main(int argc, char **argv) {
  54. short   result, i;
  55. char    *ptr, *des;
  56.     FORCEOFFHOOK = TRUE;             /* Default is FALSE                      */
  57.     USERSFILE = FALSE;               /* Default is FALSE anyway...            */
  58.     NO_FKEYS = FALSE;                /* Default is FALSE anyway...            */
  59.     DOTS = FALSE;                    /* Default is FALSE anyway...            */
  60.     ck_NO_STATUS = FALSE;            /* Enable Status line on local screen    */
  61.     silent = FALSE;                  /* Turn on local beeps(default is FALSE) */
  62.     logoff_color = (green);          /* Logoff msg to green (default=magenta) */
  63.     ck_logoff_msgs = &logoffs[0];    /* Set message table to demo msgs        */
  64.     ck_gen_msgs    = &ckit_msgs[0];  /* Set message table to demo msgs        */
  65.     set_kybd_time(2,6);              /* Beep time 2, timeout 6 minutes        */
  66.     OPENED = FALSE;
  67.     if (argc > 1) {                  /* (default is 3, 5 minutes)             */
  68.         result = open_door(argv[1], argv[2]);
  69.         if(!PCB) {
  70.             s_printf("Must use PCBOARD.SYS 14.5A or greater\n");
  71.             exit(1);
  72.         }
  73.         ck_graphics |= ck_ansi_ng;
  74.         if(!result) {
  75.             OPENED = TRUE;      /* Door has been opened               */
  76.             atexit(shut_down);  /* call shut_down at exit time        */
  77.         } else {
  78.             OPENED = FALSE;     /* Door open failed for some reason   */
  79.             exit(1);
  80.         }
  81.     }
  82.     if(read_pcbdat()) {     /* Read pcboard.dat file */
  83.         exit(1);
  84.     }
  85.     display_conferences();
  86. }
  87.  
  88. /******************************************************************************
  89.  *           Display conference information from pcboard.sys                  *
  90.  *****************************************************************************/
  91. #define  CONFS  66          // Regular conferences 0-39   offset in pcboard.sys
  92. #define  XCONFS 144         // Extended conferences 40-?? offset in pcboard.sys
  93.  
  94. void display_conferences(void) {
  95.     BYTE    *joined, *scanned;
  96.     short   HighConf, Bytes, i, XConfs;
  97.  
  98.  
  99. /* Obtain high conference number read from pcboard.dat */
  100.     HighConf = atoi(pcboard_dat.high_conference);
  101.     s_printf("\n\rTotal conferences allocated = %d", HighConf);
  102.     s_printf("\n\rLast conference joined = %d", ck_last_conf);
  103.  
  104. /* Print conferences user has joined for conferences 0-39 */
  105.     s_printf("\r\n\r\nLower 0-39 Conferences joined - ");
  106.     joined = ck_pcbsysbuf + CONFS;      /* Lower conferences buffer offsets */
  107.     scanned = joined + 5;
  108.     for(i = 0; i < 39; i++) {
  109.         if(get_mapbit(i, joined)) {
  110.             s_printf("%d ", i);
  111.         }
  112.     }
  113. /* Print conferences user has scanned for conferences 0-39 */
  114.    s_printf("\n\r\nLower 0-39 Conferences scanned - ");
  115.    for(i = 0; i < 39; i++) {
  116.         if(get_mapbit(i, scanned)) {
  117.             s_printf("%d ", i);
  118.         }
  119.     }
  120. /* Find out how many bytes used to store conference information  >= 40 */
  121.     if(HighConf > 39) {
  122.         XConfs = HighConf - 39;   /*  Find out how many Xtended conferences */
  123.         Bytes  = XConfs;
  124.         Bytes =  XConfs / 8;      /* Find number of bytes used */
  125.         if(XConfs % 8) {
  126.             Bytes += 1;
  127.         }
  128.         joined = ck_pcbsysbuf + XCONFS;  /* Xtnded conferences buffer offsets */
  129.         scanned = joined + Bytes;
  130. /* Print conferences user has joined for conferences 40 and greater */
  131.         s_printf("\n\r\nXtended Conferences joined - ");
  132.         for(i = 0; i < XConfs; i++) {
  133.             if(get_mapbit(i, joined)) {
  134.                 s_printf("%d ", i+40);
  135.             }
  136.         }
  137. /* Print conferences user has scanned for conferences 40 and greater */
  138.         s_printf("\n\r\nXtended Conferences scanned - ");
  139.         for(i = 0; i < XConfs; i++) {
  140.             if(get_mapbit(i, scanned)) {
  141.                 s_printf("%d ", i+40);
  142.             }
  143.         }
  144.     }
  145. }
  146.  
  147. /******************************************************************************
  148.  * Read a specific bit from a conference's bitmap                             *
  149.  * Returns 0 if bit is OFF, 1 if bit is on.                                   *
  150.  ******************************************************************************/
  151. BYTE get_mapbit(unsigned confno, BYTE *Bitmaps) {
  152. unsigned byteno, bitno;
  153.     byteno  = (confno >> 3);
  154.     bitno   = confno % 8;
  155.     return((*(Bitmaps+byteno) >> bitno) & 1);
  156. }
  157. /******************************************************************************
  158.  * Set  a specific bit in a conference's bitmap                               *
  159.  ******************************************************************************/
  160. void set_mapbit(unsigned confno, BYTE MapNo, BYTE flag, BYTE *Bitmaps) {
  161. unsigned byteno;
  162. unsigned bitno;
  163.     byteno = (confno >> 3);
  164.     bitno  = confno % 8;
  165.     if(flag) {
  166.         *(Bitmaps+byteno) |= (1 << bitno);
  167.     } else {
  168.         *(Bitmaps+byteno) &= 255 - (1 << bitno);
  169.     }
  170. }
  171.  
  172. /******************************************************************************
  173.  *                     Read pcboard.dat file                                  *
  174.  *****************************************************************************/
  175. short    read_pcbdat(void) {
  176. char    *buffer;
  177. short   pcbdat_handle;
  178. unsigned char i;
  179. size_t  bytes_read;
  180. short   lines_read;
  181.     pathptr = getenv("PCBDAT");  /* For the newer pcboard 14.5 */
  182.     if(pathptr) {
  183.         buffer = create_buffer(BUFFER_SIZE);
  184.         pcbdat_handle = open_file(pathptr);
  185.         if(!pcbdat_handle) {
  186.             s_printf("\aError opening %s!\n\r", pathptr);
  187.             return(1);
  188.         }
  189.         bytes_read = read_record(pcbdat_handle, buffer, BUFFER_SIZE);
  190.         if(!bytes_read) {
  191.             s_printf("\aError reading %s!\n\r", pathptr);
  192.             return(1);
  193.         }
  194.         _dos_close(pcbdat_handle);
  195.         lines_read = parse_to_C(buffer, bytes_read);
  196.         copy_buffer(buffer, dat_ptrs, pcb_maxbytes);
  197.         free(buffer);
  198.         return(0);
  199.     }
  200.     s_printf("\aNo PCBDAT found in enviroment!\n\r");
  201.     return(1);
  202. }
  203.  
  204. /*****************************************************************************
  205.  *  Open a file for read only, share deny none                               *
  206.  *****************************************************************************/
  207. short  open_file(char *filename) {
  208.  
  209.     short filehandle;
  210.                              /* RDONLY */
  211.     if ((_dos_open(filename, O_RDONLY | SH_DENYNO, &filehandle)) != 0) {
  212.         return(FALSE);
  213.     }
  214.     return(filehandle);
  215. }
  216. /*****************************************************************************
  217.  *  Read buffersize bytes from file to buffer pointer                        *
  218.  *****************************************************************************/
  219. size_t read_record(short filehandle, BYTE *record, size_t readcount) {
  220.  
  221.     size_t    bytes_read;
  222.  
  223.     if ((_dos_read(filehandle, record, readcount, &bytes_read)) != 0) {
  224.         return(FALSE);
  225.     } else {
  226.         return(bytes_read);
  227.     }
  228. }
  229. /*****************************************************************************
  230.  *  Allocate a buffer for file use                                           *
  231.  *****************************************************************************/
  232. char * create_buffer(size_t size) {
  233.  
  234.     char *buffer;
  235.  
  236.     if ((buffer = (char *)malloc(size)) == NULL) {
  237.         s_printf("Unable to allocate memory\n");
  238.         return(FALSE);
  239.     }
  240.     return(buffer);
  241. }
  242. /*****************************************************************************
  243.  * Parse strings ending (with 0D/AH) to NULL terminated 'C' strings          *
  244.  * Enter with buffer to parse and size of buffer.                            *
  245.  * It will write the C strings back to the same buffer it is reading from    *
  246.  * and return the number of line converted (strings)                         *
  247.  *****************************************************************************/
  248. short   parse_to_C(register char *buffer, size_t bytes_to_do) {
  249. register char    *buffer1;
  250. short   line_number = 0;
  251.  
  252.     buffer1 = buffer;
  253.     while(bytes_to_do != 0) {
  254.         if(*buffer != 0x0D && *buffer != 0x1A) {  /* Check if C/R or EOF  */
  255.                 *buffer1++ = *buffer++; /* copy byte                        */
  256.                 bytes_to_do -= 1;       /* Decrement number of bytes to do  */
  257.         } else {
  258.             if(*++buffer == 0x0A) {     /* Check for line feed              */
  259.                 *buffer = '\0';         /* Terminate string with NULL       */
  260.                 line_number += 1;       /* Increment line counter           */
  261.             } else {
  262.                 *buffer1++ = *buffer;
  263.             }
  264.             bytes_to_do -= 1;           /* Decrement number of bytes to do  */
  265.         }
  266.     }
  267.     return(line_number);                /* Return number of lines read      */
  268. }
  269.  
  270. /*****************************************************************************
  271.  * Copy a block of strings in memory to strings allocated for the strings.   *
  272.  * Enter with buffer to read strings from, array of pointers to the strings, *
  273.  * and an array containing the max. bytes allowed for the strings.           *
  274.  *****************************************************************************/
  275. void    copy_buffer(char *buffer, char **dat_ptr, char *pcb_maxbytes) {
  276.  
  277. unsigned char i;
  278. char    *pcbdata_ptr;
  279.  
  280.     for (i=0; i <= ( (sizeof(dat_ptrs) / sizeof(char *) ) - 1); i++) {
  281.         pcbdata_ptr = dat_ptr[i];
  282.         buffer = str_move(pcbdata_ptr, pcb_maxbytes[i], buffer);
  283.     }
  284. }
  285.  
  286. /*****************************************************************************
  287.  *  Copy a string from memory to a string location.  Enter with pointer to   *
  288.  *  string destination, string source and max. number of bytes allowed for   *
  289.  *  the string. (size of string).  If exceeded, it will terminate string     *
  290.  *  with a NULL. Returns a pointer equal to next one plus string length.     *
  291.  *****************************************************************************/
  292. char *  str_move(register char *data_ptr, unsigned char max_bytes, register char *buffer) {
  293.     if(max_bytes >= 1) {
  294.         max_bytes -= 1;
  295.         while(*buffer != '\0' && max_bytes != 0) {
  296.             *data_ptr++ = *buffer++;
  297.             max_bytes -= 1;
  298.         }
  299.     }
  300.     *data_ptr = '\0';
  301.     *buffer++;
  302.     return(buffer);
  303. }
  304. /****************************************************************************
  305.  *      Close door, reset vectors etc.                                      *
  306.  *  Need in case of a run time error occurs in door                         *
  307.  ****************************************************************************/
  308.  void   shut_down(void) {
  309.     if(OPENED && !CLOSED) {
  310.         close_door();
  311.         CLOSED = TRUE;
  312.     }
  313. }
  314. /****************************************************************************
  315.  *      s_printf() function for sending output to port                        *
  316.  ****************************************************************************/
  317. void s_printf(char *s_format,...) {
  318.     va_list arg_pointer;
  319.     char buffer[256];
  320.     va_start(arg_pointer, s_format);
  321.     vsprintf(buffer, s_format, arg_pointer);
  322.     s_puts(buffer);
  323.     va_end(arg_pointer);
  324. }
  325. /****************************************************************************/
  326. /************************ E N D  O F   M O D U L E **************************/
  327.  
  328.